package com.ruoyi.web.controller.zhuangxiucontroller;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.system.domain.zhuangxiumain.orderformMain.OrderRetreat;
import com.ruoyi.system.domain.zhuangxiumain.shopMain.SupplierInfo;
import com.ruoyi.system.service.vantservice.orderservice.ApiOrderRetreatService;
import com.ruoyi.system.service.zhaungxiuservice.shopService.SupplierInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/orretreat/retreat")
public class OrderRetreatController extends BaseController {

    @Autowired
    private ApiOrderRetreatService orderRetreatService;
    @Autowired(required = false)
    private TokenService tokenService;
    @Autowired
    private SupplierInfoService supplierInfoService;



    /**
     * 查询申请退货列表
     */
//    @PreAuthorize("@ss.hasPermi('system:retreat:list')")
    @GetMapping("/list")
    public TableDataInfo list(OrderRetreat orderRetreat)
    {
        startPage();
        List<OrderRetreat> orderRetreats = new ArrayList<>();
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        Long userId = loginUser.getUser().getUserId();

        SupplierInfo supplierInfo = new SupplierInfo();
        supplierInfo.setCustomerId(userId);
        List<SupplierInfo> allUserId = supplierInfoService.findAllUserIdOne(supplierInfo);
        for(SupplierInfo allUserIds:allUserId){
            orderRetreat.setSupplierId(allUserIds.getSupplierId());
            List<OrderRetreat> list = orderRetreatService.selectOrderRetreatList(orderRetreat);
            orderRetreats.addAll(list);
        }


        return getDataTable(orderRetreats);
    }



    /**
     * 获取申请退货详细信息
     */

    @GetMapping(value = "/getInfo/{orderRetreatId}")
    public AjaxResult getInfo(@PathVariable("orderRetreatId") Integer orderRetreatId)
    {
        return AjaxResult.success(orderRetreatService.orderRetreatById(orderRetreatId));
    }

    @GetMapping("/selectById/{orderId}")
    public AjaxResult selectOrderRetreatById(@PathVariable("orderId") Integer orderId){
        return AjaxResult.success(orderRetreatService.selectById(orderId));
    }


    /**
     * 修改申请退货
     */

    @PutMapping("/edit")
    public AjaxResult edit(@RequestBody OrderRetreat orderRetreat)
    {
        return toAjax(orderRetreatService.updateOrderRetreat(orderRetreat));
    }




}
